<?php


namespace app\api\controller;

use think\exception\ValidateException;
use app\api\BaseController;
use app\api\validate\Discount as Validate;
use think\App;
require_once '/www/wwwroot/stop/public/plugins/pay/addons/wechat/lib/WxPay.Api.php';
require_once '/www/wwwroot/stop/public/plugins/pay/addons/wechat/example/WxPay.NativePay.php';
require_once '/www/wwwroot/stop/public/plugins/pay/addons/wechat/example/WxPay.JsApiPay.php';
require_once '/www/wwwroot/stop/public/plugins/pay/addons/wechat/example/WxPay.Config.php';

class Discount extends BaseController
{
    /**
     * 显示资源列表
     * 访问：local.stop.com/api/coupon/index
     */
    public function user_list(App $app)
    {
        if ($this->request->isPost()) {
            $input = input("post.");
            $page = input("post.page");
            $page_size = input("post.page_size");
            $page = $page ? $page : 1;
            $page_size = $page_size ? $page_size : 15;
            if (empty($this->request->userId)) {
                return json(['status' => 'login', 'message' => '请登录后操作！']);
            }
            $count = (new \app\admin\model\Discount())->where('user_id', $this->request->userId)->count();
            $list = (new \app\admin\model\Discount())->where('user_id', $this->request->userId)->page($page, $page_size)->select()->toArray();
            $totalPages = ceil($count / $page_size);
            $data['list'] = $list;
            $data['count'] = $count;
            $data['totalPages'] = $totalPages;
            return json(["status" => "success", "message" => "请求成功", "data" => $data]);
        }
    }



    public function sign_list(App $app)
    {
        if ($this->request->isPost()) {
            $userData = (new \app\api\model\User())->where('id', $this->request->userId)->findOrEmpty()->toArray();
            $stopUser = (new \app\api\model\StopUser())->where('phone', $userData['mobile'])->where('status', 1)->findOrEmpty()->toArray();
            if (empty($stopUser)) {
                return json(["status" => "error", "message" => "权限不足"]);
            }
            $input = input("post.");
            $page = input("post.page");
            $page_size = input("post.page_size");
            $page = $page ? $page : 1;
            $page_size = $page_size ? $page_size : 15;
            if (empty($this->request->userId)) {
                return json(['status' => 'login', 'message' => '请登录后操作！']);
            }
            $count = (new \app\admin\model\Discount())->where('sign_user_id', $this->request->userId)->count();
            $list = (new \app\admin\model\Discount())->where('sign_user_id', $this->request->userId)->page($page, $page_size)->select()->toArray();
            $totalPages = ceil($count / $page_size);
            $data['list'] = $list;
            $data['count'] = $count;
            $data['totalPages'] = $totalPages;
            return json(["status" => "success", "message" => "请求成功", "data" => $data]);
        }
    }


    public function detail(App $app)
    {
        if ($this->request->isPost()) {
            $input = input("post.");
            $data = (new \app\admin\model\Discount())->where('id', $input['id'])->findOrEmpty()->toArray();
            return json(["status" => "success", "message" => "success", 'data' => $data]);
        }
    }


    /**
     * 保存新建的资源
     */
    public function save()
    {
        if ($this->request->isPost()) {
            try {
                $userData = (new \app\api\model\User())->where('id', $this->request->userId)->findOrEmpty()->toArray();
                $stopUser = (new \app\api\model\StopUser())->where('phone', $userData['mobile'])->where('status', 1)->findOrEmpty()->toArray();
                if (empty($stopUser)) {
                    return json(["status" => "error", "message" => "权限不足"]);
                }
                $data = input("post.");
                validate(Validate::class)->scene("save")->check($data);
                $save['start_time'] = $data['start_time'];
                $save['end_time'] = $data['end_time'];
                $save['sign_user_id'] = $this->request->userId;
                $save['merchant_id'] = $stopUser['merchant_id'];
                $save['status'] = 0;
                $save['amount'] = $data['amount'];
                $save['cill_amount'] = $data['cill_amount'];
                if($save['cill_amount']<=$save['amount']){
                    return json(["status" => "error", "message" => "优惠金额必须小于门槛"]);
                }
                $id = (new \app\admin\model\Discount())->insertGetId($save);
            } catch (ValidateException $e) {
                return json(["status" => "error", "message" => $e->getError()]);
            }
            return json(["status" => "success", "message" => "success"]);
        }
    }


    /**
     * 删除指定资源
     */
    public function delete()
    {
        $userData = (new \app\api\model\User())->where('id', $this->request->userId)->findOrEmpty()->toArray();
        $stopUser = (new \app\api\model\StopUser())->where('phone', $userData['mobile'])->where('status', 1)->findOrEmpty()->toArray();
        if (empty($stopUser)) {
            return json(["status" => "error", "message" => "权限不足"]);
        }
        $data = input("post.");
        $order = (new \app\admin\model\Discount())->where('id', $data['id'])->findOrEmpty()->toArray();
        if (empty($order)) {
            return json(['status' => 'error', 'message' => '已删除请勿重复操作']);
        }
        if ($order['user_id'] > 0) {
            return json(['status' => 'error', 'message' => '已被领取请勿重复操作']);
        }
        (new \app\admin\model\Discount())->where('id', $data['order_id'])->delete();
        return json(['status' => 'success', 'message' => '操作成功']);

    }

    public function give()
    {
        if ($this->request->isPost()) {
            try {
                $input = input("post.");
                if (empty($this->request->userId)) {
                    return json(['status' => 'login', 'message' => '请登录后操作！']);
                }
                $coupon = (new \app\admin\model\Discount())->where('id', $input['id'])->findOrEmpty()->toArray();
                if (empty($coupon)) {
                    return json(["status" => "error", "message" => "优惠券不存在"]);
                }
                if ($coupon['user_id'] != 0) {
                    return json(["status" => "error", "message" => "兑换码已被领取"]);
                }
                if (date('Y-m-d H:i:s', time()) > $coupon['end_time']) {
                    return json(["status" => "error", "message" => "兑换码已过期"]);
                }
                (new \app\admin\model\Discount())->where('id', $input['id'])->update(['user_id' => $this->request->userId, 'status' => 1]);
                return json(["status" => "success", "message" => "领取成功"]);
            } catch (ValidateException $e) {
                return json(["status" => "error", "message" => $e->getError()]);
            }
        }
    }
}